// ----------------------------------
// RSDK Project: Sonic Essence
// Script Description: Sign Post Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// Aliases
private alias 21 : TYPE_SIGNPOST

private alias 0 : IDLE
private alias 1 : SPIN
private alias 2 : ACTCLEAR
private alias 3 : IDLE_ACTIVE


private alias object.value6 : object.timer
private alias object.value8 : object.touch
private alias object.value9 : playSound

// Function declarations
reserve function SignPost_DebugDraw
reserve function SignPost_DebugSpawn

// Static Values

// Tables

function SignPost_DebugDraw
	DrawSprite(0)
end function


function SignPost_DebugSpawn
	CreateTempObject(TypeName[Sign Post], 0, object.xpos, object.ypos)
	object[tempObjectPos].frame = 1
	object[tempObjectPos].value1 = 16
	if options.vsMode == 1
		object[tempObjectPos].state = 3
	end if
end function




event ObjectMain
	switch object.state
	case IDLE
		object.timer++
		if object.timer == 4
			object.timer = 0
			object.frame++
			if object.frame == 8
				object.frame = 0
			end if
		end if
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				BoxCollisionTest(C_TOUCH, object.entityPos, -20, -255, 20, 20, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				if checkResult == true
					object.touch = 1
				endif
				BoxCollisionTest(C_TOUCH, object.entityPos, -302, -102, 302, 102, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				if checkResult == true
					if stage.state != STAGE_PAUSED
						if playSound == false
							PlaySfx(SfxName[Goal Ring Near], 1)
							playSound = true
						endif
					else
						StopSfx(SfxName[Goal Ring Near])
						playSound = false
					endif
				else
					StopSfx(SfxName[Goal Ring Near])
					playSound = false
				end if
			next
		temp0 = object.ypos
		temp0 += 0x400000
		if object[0].ypos < temp0
			stage.newXBoundary1 = object.xpos
			stage.newXBoundary1 >>= 16
			stage.newXBoundary1 -= screen.xcenter
			stage.newXBoundary1 -= screen.xcenter
			stage.newXBoundary2 = object.xpos
			stage.newXBoundary2 >>= 16
			stage.newXBoundary2 += screen.xcenter
			//if object[0].xpos > object.xpos
			if object.touch == 1
				stage.newXBoundary1 = object.xpos
				stage.newXBoundary1 >>= 16
				stage.newXBoundary1 -= screen.xcenter
				if stage.debugMode == 0
					if stage.activeList == BONUS_STAGE
						if stage.minutes < 7
							CallNativeFunction2(SetAchievement, 10, 100)
						end if
					else
						if stage.listPos == 0
							if stage.minutes == 0
								if stage.seconds < 35
									CallNativeFunction2(SetAchievement, 0, 100)
								end if
							end if
						end if
					end if
				end if
				stage.timeEnabled = 0
				object.timer = 0
				object.frame = 0
				object.state = SPIN
				PlaySfx(SfxName[Sign Post], 0)
				if PlayerObject_SuperState == 1
					PlayerObject_SuperState = 2
					if stage.debugMode == 0
						CallNativeFunction2(SetAchievement, 3, 100)
					end if
				end if
			end if
		end if
		break
	case SPIN
		StopSfx(SfxName[Goal Ring Near], 1)
		music.volume--
		object.value2++
		if object.value2 == 2
			object.value2 = 0
			object.frame++
			if object.frame == 8
				object.frame = 0
				Rand(temp0, 48)
				temp0 <<= 16
				temp0 += object.xpos
				temp0 -= 0x180000
				Rand(temp1, 32)
				temp1 <<= 16
				temp1 += object.ypos
				temp1 -= 0x140000
				CreateTempObject(TypeName[Ring Sparkle], 0, temp0, temp1)
				object.value1--
				if object.value1 == 0
					SetBit(object.value3, 0, 1)
					if stage.player2Enabled == 1
						SetBit(object.value3, 1, 1)
					end if
					object.state = ACTCLEAR
				end if
			end if
		end if
		break
	case ACTCLEAR
		temp0 = screen.xcenter
		temp0 <<= 16
		temp0 += object.xpos
			if options.gamemode == 0
				PlayMusic(1)
			else
				SetMusicTrack("RESULTS.ogg",0,1)
				PlayMusic(0)	
			end if
			if options.gamemode == 2
				SetMusicTrack("RESULTS_CH.ogg",0,0)
				PlayMusic(0)
			end if
			object.state = IDLE_ACTIVE
			object[30].type = TypeName[Act Finish]
			object[30].drawOrder = 6
			object[30].priority = PRIORITY_ACTIVE
		break

	case IDLE_ACTIVE
		object.timer++
		if object.timer == 4
			object.timer = 0
			object.frame++
			if object.frame > 7
				object.frame = 0
			end if
		end if
		break
	end switch
	temp0 = 40
	temp0 += 212
	temp0 <<= 16
	temp0 += object.xpos
	currentPlayer = 0
	while currentPlayer < playerCount
		GetBit(temp1, object.value3, currentPlayer)
		if temp1 == 1
			object[currentPlayer].interaction = 0
			object[currentPlayer].controlMode = -1
			object[currentPlayer].up = 0
			object[currentPlayer].down = 0
			object[currentPlayer].left = 0
			object[currentPlayer].right = 0
			object[currentPlayer].jumpPress = 0
			object[currentPlayer].jumpHold = 0
		end if
		if object[currentPlayer].type == TypeName[Debug Mode]
			SetBit(object.value3, currentPlayer, 0)
		end if
		currentPlayer++
	loop
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				temp0 = object[currentPlayer].collisionRight
				temp0 <<= 16
				temp0 += object[currentPlayer].xpos
				temp1 = stage.curXBoundary2
				temp1 <<= 16
				if temp0 > temp1
					object[currentPlayer].xvel = 0
					object[currentPlayer].speed = 0
					object[currentPlayer].xpos = temp1
					temp0 = object[currentPlayer].collisionRight
					temp0 <<= 16
					object[currentPlayer].xpos -= temp0
				end if
		next
end event


event ObjectDraw
	// All rings use the same frame
	// (Animation is handled by Stage Setup object)
	DrawSprite(object.frame)
end event


event ObjectStartup
	LoadSpriteSheet("Global/Items.gif")
	SpriteFrame(-24, -24, 48, 48, 181, 258)
	SpriteFrame(-24, -24, 48, 48, 230, 258)
	SpriteFrame(-24, -24, 48, 48, 279, 258)
	SpriteFrame(-24, -24, 48, 48, 328, 258)
	SpriteFrame(-24, -24, 48, 48, 181, 307)
	SpriteFrame(-24, -24, 48, 48, 230, 307)
	SpriteFrame(-24, -24, 48, 48, 279, 307)
	SpriteFrame(-24, -24, 48, 48, 328, 307)
	foreach (TypeName[Sign Post], arrayPos0, ALL_ENTITIES)
		CheckGreater(object[arrayPos0].propertyValue, 0)
		temp0 = checkResult
		CheckEqual(options.vsMode, 0)
		temp0 &= checkResult
		if temp0 == 1
			object[arrayPos0].type = TypeName[Blank Object]
		else
			object[arrayPos0].frame = 1
			object[arrayPos0].value1 = 7
			if object[arrayPos0].propertyValue != 2
				object[arrayPos0].priority = PRIORITY_XBOUNDS
			end if
		end if
		if options.vsMode == 1
			object[arrayPos0].state = 3
		end if
	next
	SetTableValue(TypeName[Sign Post], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(SignPost_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(SignPost_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
